#coding=utf-8
from django.shortcuts import render
from django.http import HttpResponse
from models import Users
# from models import UserHeader
from . import indexView
# Create your views here.
# def login(request):
# 	return render(request,"login.html")

# 	pass

def login(request):
	# POST 要大写
	context={'msg':''}
	if request.method == 'POST':
		username=request.POST['username']
		password=request.POST['password']
		
		# 查询数据库
		userArr = Users.objects.filter(username=username)
		if userArr:
			# 用户名存在
			if userArr[0].password == password:
				# 验证通过跳转到登陆成功页面
				# techers = Users.objects.filter(identity=1)
				# userinfo={'userid':userArr[0].id,'techers':techers}
				# context={'userinfo':userinfo}
				# return render(request,'index.html',context)
				request.session["id"]=userArr[0].id
				request.session.set_expiry(0)
				return indexView.index(request)
			else:
				context={'msg':'用户名或密码错误'}
		else:
			# 用户名不存在
			context={'msg':'用户名不存在'}
	return render(request,"login.html",context)

def register(request):
	if request.method=='POST':
		username=request.POST['username']
		password=request.POST['psw1']
		nickname=request.POST['nickname']
		identity=request.POST['identity']
		sex=request.POST['sex']
		age=int(request.POST['age'])
		hobby=request.POST['hobby']
		eduBackgroud=request.POST['eduBackgroud']
		school=request.POST['school']
		grade=request.POST['grade']
		reward=request.POST['reward']
		englishLevel=request.POST['englishLevel']
		address=request.POST['address']
		contact=request.POST['contact']
		user=Users(src='header/11.jpg',username=username,password=password,nickname=nickname,identity=identity,sex=sex,hobby=hobby,eduBackgroud=eduBackgroud,school=school,address=address,age=age,grade=grade,reward=reward,englishLevel=englishLevel,contact=contact)
		user.save()
		return render(request,"login.html")
	return render(request,"register.html")
	pass
#ajax请求的响应
def checkUsername(request):
	username=request.POST['username']
	#查询前端传过来的用户名是否在数据库中
	res=Users.objects.filter(username=username)
	return HttpResponse(res)
	pass